package com.property.cleaning.service.impl;

import com.property.cleaning.common.Result;
import com.property.cleaning.common.ResultCode;
import com.property.cleaning.entity.Area;
import com.property.cleaning.mapper.AreaMapper;
import com.property.cleaning.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 区域服务实现类
 * 
 * @author Property Cleaning System
 */
@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaMapper areaMapper;

    @Override
    public Result<Area> createArea(Area area) {
        if (area == null || area.getAreaName() == null || area.getAreaName().trim().isEmpty()) {
            return Result.error(ResultCode.PARAM_ERROR, "区域名称不能为空");
        }
        
        areaMapper.insert(area);
        return Result.success(area);
    }

    @Override
    public Result<Area> updateArea(Area area) {
        if (area == null || area.getId() == null) {
            return Result.error(ResultCode.PARAM_ERROR, "区域ID不能为空");
        }
        
        Area existingArea = areaMapper.selectById(area.getId());
        if (existingArea == null) {
            return Result.error(ResultCode.USER_NOT_FOUND, "区域不存在");
        }
        
        areaMapper.updateById(area);
        return Result.success(area);
    }

    @Override
    public Result<Void> deleteArea(Long areaId) {
        if (areaId == null) {
            return Result.error(ResultCode.PARAM_ERROR, "区域ID不能为空");
        }
        
        areaMapper.deleteById(areaId);
        return Result.success();
    }

    @Override
    public Result<Area> getAreaById(Long areaId) {
        if (areaId == null) {
            return Result.error(ResultCode.PARAM_ERROR, "区域ID不能为空");
        }
        
        Area area = areaMapper.selectById(areaId);
        if (area == null) {
            return Result.error(ResultCode.USER_NOT_FOUND, "区域不存在");
        }
        
        return Result.success(area);
    }

    @Override
    public Result<com.baomidou.mybatisplus.core.metadata.IPage<Area>> getAreaPage(com.baomidou.mybatisplus.extension.plugins.pagination.Page<Area> page, String keyword) {
        return Result.success(null);
    }

    @Override
    public Result<List<Area>> getAreaTree() {
        List<Area> areas = areaMapper.findAreaTree();
        return Result.success(areas);
    }

    @Override
    public Result<List<Area>> getChildrenAreas(Long parentId) {
        if (parentId == null) {
            return Result.error(ResultCode.PARAM_ERROR, "父区域ID不能为空");
        }
        
        List<Area> children = areaMapper.findByParentId(parentId);
        return Result.success(children);
    }

    @Override
    public Result<List<Area>> getRootAreas() {
        List<Area> rootAreas = areaMapper.findRootAreas();
        return Result.success(rootAreas);
    }

    @Override
    public Result<Void> setAreaManager(Long areaId, Long managerId) {
        if (areaId == null || managerId == null) {
            return Result.error(ResultCode.PARAM_ERROR, "区域ID和负责人ID不能为空");
        }
        
        Area area = areaMapper.selectById(areaId);
        if (area == null) {
            return Result.error(ResultCode.USER_NOT_FOUND, "区域不存在");
        }
        
        area.setManagerId(managerId);
        areaMapper.updateById(area);
        return Result.success();
    }
}